package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.AnalysisRegion;
import com.hunttown.mes.rpc.domain.AnalysisRegionDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import com.hunttown.mes.rpc.exception.ConvertException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2018-11-26 15:06:32
 */
public class AnalysisRegionConverts {

    private static Logger LOG = LoggerFactory.getLogger(AnalysisRegionConverts.class);

    public static List<AnalysisRegionDTO> convert(List<AnalysisRegion> analysisRegionList) {
        if (CollectionUtils.isEmpty(analysisRegionList)) {
            return Collections.EMPTY_LIST;
        }

        List<AnalysisRegionDTO> analysisRegionListResult = Lists.newArrayList();
        for (AnalysisRegion analysisRegion : analysisRegionList) {
            analysisRegionListResult.add(convert(analysisRegion));
        }
        return analysisRegionListResult;
    }

    public static AnalysisRegionDTO convert(AnalysisRegion analysisRegion) {
        try {
            return BeanUtils.copy(analysisRegion, AnalysisRegionDTO.class);
        } catch (Exception e) {
            LOG.error("AnalysisRegion到AnalysisRegionDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisRegion到AnalysisRegionDTO转换失败", e);
        }
    }

    public static AnalysisRegion convert(AnalysisRegionDTO analysisRegionDTO) {
        try {
            return BeanUtils.copy(analysisRegionDTO, AnalysisRegion.class);
        } catch (Exception e) {
            LOG.error("AnalysisRegionDTO到AnalysisRegion转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisRegionDTO到AnalysisRegion转换失败", e);
        }
    }
}